package com.dnjn.happiness.emo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.domain.UserRecordTask;
import com.dnjn.happiness.emo.domain.UserRecordTaskTimeline;
import com.dnjn.happiness.emo.domain.vo.UserRecordTaskTimelineVO;
import com.dnjn.happiness.emo.service.IUserRecordTaskService;
import com.dnjn.happiness.emo.service.IUserRecordTaskTimelineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 任务状态记录Controller
 *
 * @author amber
 * @date 2024-01-12
 */
@Tag(name = "emo userRecordTaskTimeline 任务状态记录", description = "任务状态记录")
@RestController
@RequestMapping("/userRecordTaskTimeline")
public class UserRecordTaskTimelineController extends BaseController {
    @Autowired
    private IUserRecordTaskTimelineService userRecordTaskTimelineService;

    @Autowired
    private IUserRecordTaskService userRecordTaskService;


    /**
     * 查询任务状态记录列表
     */
    //@RequiresPermissions("emo:userRecordTaskTimeline:list")
    @PostMapping("/list")
    @Log(title = "emo", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody UserRecordTaskTimelineVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        UserRecordTaskTimeline userRecordTaskTimeline = new UserRecordTaskTimeline();
        BeanUtils.copyPropsIgnoreNull(vo, userRecordTaskTimeline);
        Page page = Page.of(current, size);
        Page<UserRecordTaskTimeline> list = userRecordTaskTimelineService.page(page, Wrappers.query(userRecordTaskTimeline));
        return success(list);
    }
    /**
     * 获取任务状态记录详细信息
     */
    //@RequiresPermissions("emo:userRecordTaskTimeline:query")
    @GetMapping(value = "/{taskTimelineId}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("taskTimelineId") Long taskTimelineId) {
        return success(userRecordTaskTimelineService.getById(taskTimelineId));
    }

    /**
     * 新增任务状态记录
     */
    //@Log(title = "任务状态记录", businessType = BusinessType.INSERT)
    //@RequiresPermissions("emo:userRecordTaskTimeline:add")
    @PostMapping("/add")
    @Operation(summary = "接受任务(接受任务 B_RECEIVED,待打卡),打卡,反馈", description = "任务状态 A_APPLIED(待接收) B_RECEIVED(待打卡) C_CHECKED_IN(待反馈) D_FEEDBACK_GIVEN(待评价) F_DONE(已结束),\n" +
            "\n" +
            "接收" +
            "{\n" +
            "  \"taskId\": \"1748169689030610946\",\n" +
            "  \"taskStatus\": \"\"\n" +
            "}" +
            "仅仅打卡" +
            "{" +
            "  \n" +
            "  \"taskId\": \"1748169689030610946\",\n" +
            "  \"recordStatus\": \"OK\",\n" +
            "  \"taskStatus\": \"\",\n" +
            "   \"type\": \"C\"\n" +
            " \n" +
            "}" +
            "仅仅反馈" +
            "{\n" +
            "  \"content\": \"11\",\n" +
            "  \"taskId\": \"1748169689030610946\",\n" +
            "  \"recordStatus\": \"OK\",\n" +
            "  \"taskStatus\": \"\",\n" +
            "   \"type\": \"D\"\n" +
            " \n" +
            "}" +
            "" +
            "同时提交" +
            "{\n" +
            " \"content\": \"内容\",\n" +
            "  \"taskId\": \"1748169689030610946\",\n" +
            "  \"recordStatus\": \"OK\",\n" +
            "  \"taskStatus\": \"\",\n" +
            "   \"type\": \"sameTime\"\n" +
            " \n" +
            "}" )
    public AjaxResult add(@RequestBody UserRecordTaskTimelineVO vo) {
        UserRecordTaskTimeline userRecordTaskTimeline = new UserRecordTaskTimeline();
        BeanUtils.copyPropsIgnoreNull(vo, userRecordTaskTimeline);

        Assert.notNull(vo.getTaskStatus(), "任务状态不能为空");
        LocalDateTime now = LocalDateTime.now();
        userRecordTaskTimeline.setDateCurr(now);
        if (null != vo.getType() && vo.getType().equals("D")) {
            // 反馈 反馈内容必须填写  仅仅反馈
            Assert.notNull(vo.getContent(), "反馈内容不能为空");
            userRecordTaskTimeline.setCreateBy(SecurityUtils.getUsername());
            userRecordTaskTimeline.setTaskStatus("D_FEEDBACK_GIVEN");
            boolean save = userRecordTaskTimelineService.save(userRecordTaskTimeline);
            if (save) {
                userRecordTaskService.lambdaUpdate().eq(UserRecordTask::getTaskId, vo.getTaskId()).set(UserRecordTask::getTaskStatus, vo.getTaskStatus()).update();
                return success();
            } else {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("warn", "保存失败!");
                return ajaxResult;
            }
        }
        if (null != vo.getType() && vo.getType().equals("C")) {
            //打卡 备注 内容可以为空  仅仅打卡
            userRecordTaskTimeline.setCreateBy(SecurityUtils.getUsername());
            userRecordTaskTimeline.setContent(null);
            userRecordTaskTimeline.setTaskStatus("C_CHECKED_IN");
            boolean save = userRecordTaskTimelineService.save(userRecordTaskTimeline);
            if (save) {
                userRecordTaskService.lambdaUpdate().eq(UserRecordTask::getTaskId, vo.getTaskId()).set(UserRecordTask::getTaskStatus, vo.getTaskStatus()).update();
                return success();
            } else {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("warn", "保存失败!");
                return ajaxResult;
            }
        }
        if (null != vo.getType() && vo.getType().equals("sameTime")) {
            //同时提交
            Assert.notNull(vo.getContent(), "反馈内容不能为空");
            UserRecordTaskTimeline userRecordTaskTimeline1 = new UserRecordTaskTimeline();
            LocalDateTime now1 = LocalDateTime.now();
            userRecordTaskTimeline1.setDateCurr(now1);

            //保存打卡
            BeanUtils.copyPropsIgnoreNull(vo, userRecordTaskTimeline1);
            userRecordTaskTimeline1.setTaskStatus(" C_CHECKED_IN ");
            userRecordTaskTimeline1.setCreateBy(SecurityUtils.getUsername());
            userRecordTaskTimeline1.setContent(null);
            boolean save1 = userRecordTaskTimelineService.save(userRecordTaskTimeline1);
            //保存反馈
            UserRecordTaskTimeline userRecordTaskTimeline2 = new UserRecordTaskTimeline();
            BeanUtils.copyPropsIgnoreNull(vo, userRecordTaskTimeline2);
            LocalDateTime now2 = LocalDateTime.now();
            userRecordTaskTimeline2.setDateCurr(now2);
            userRecordTaskTimeline2.setCreateBy(SecurityUtils.getUsername());
            userRecordTaskTimeline2.setTaskStatus("D_FEEDBACK_GIVEN");

            boolean save2 = userRecordTaskTimelineService.save(userRecordTaskTimeline2);
            if (save2) {
                userRecordTaskService.lambdaUpdate().eq(UserRecordTask::getTaskId, vo.getTaskId()).set(UserRecordTask::getTaskStatus, "D_FEEDBACK_GIVEN").update();
                return success();
            } else {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("warn", "同时提交保存失败!");
                return ajaxResult;
            }
        } else {
            //接受
            userRecordTaskTimeline.setTaskStatus("B_RECEIVED");
            boolean save = userRecordTaskTimelineService.save(userRecordTaskTimeline);
            if (save) {
                userRecordTaskService.lambdaUpdate().eq(UserRecordTask::getTaskId, vo.getTaskId()).set(UserRecordTask::getTaskStatus, vo.getTaskStatus()).update();
                return success();
            } else {
                AjaxResult ajaxResult = new AjaxResult();
                ajaxResult.put("warn", "保存失败!");
                return ajaxResult;
            }

        }


    }

    /**
     * 修改任务状态记录
     */
    //@Log(title = "任务状态记录", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("emo:userRecordTaskTimeline:edit")
    @PutMapping("/edit")
    @Operation(summary = "修改任务状态记录")
    public AjaxResult edit(@RequestBody UserRecordTaskTimelineVO vo) {
        UserRecordTaskTimeline userRecordTaskTimeline = new UserRecordTaskTimeline();
        BeanUtils.copyPropsIgnoreNull(vo, userRecordTaskTimeline);
        return toAjax(userRecordTaskTimelineService.updateById(userRecordTaskTimeline));
    }

    /**
     * 删除任务状态记录
     */
    //@Log(title = "任务状态记录", businessType = BusinessType.DELETE)
    //@RequiresPermissions("emo:userRecordTaskTimeline:remove")
    @DeleteMapping("/{taskTimelineIds}")
    public AjaxResult remove(@PathVariable Long[] taskTimelineIds) {
        return toAjax(userRecordTaskTimelineService.removeBatchByIds(Arrays.asList(taskTimelineIds)));
    }
}
